package com.qiaotongtianxia.loans.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.bean.LoginBean;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.SPUtil;
import com.qiaotongtianxia.loans.views.BaseEditText;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.ButtonObserver;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/11/18.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class LoginActivity extends BaseActivity {

    private final int REGISTE = 100;

    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.et_account)
    AutoCompleteTextView etAccount;
    @BindView(R.id.iv_accountClear)
    ImageView ivAccountClear;
    @BindView(R.id.et_password)
    BaseEditText etPassword;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.btn_login)
    ButtonObserver btnLogin;


    @BindView(R.id.cb_rememberPw)
    CheckBox cbRememberPw;
    @Override
    public int getLayoutId() {
        return R.layout.login_activity;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        //给输入框设置数据

        Map<String, String> accountMap = SPUtil.getAll(this, SPUtil.ACCOUNTS);
        ArrayList<String> accounts = new ArrayList<>();
        accounts.addAll(accountMap.values());
        etAccount.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, accounts));
        etAccount.setDropDownHorizontalOffset(-50);
        etAccount.setThreshold(1);

        btnLogin.observers(etAccount, etPassword);
        init();
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void initTitle() {
        ivNavBack.setVisibility(View.GONE);
        tvNavTitle.setText(getString(R.string.login));
    }

    private void init() {
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String account = s.toString();
                if (TextUtils.isEmpty(account)) {
                    ivAccountClear.setVisibility(View.GONE);
                } else {
                    ivAccountClear.setVisibility(View.VISIBLE);
                }
            }
        });
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String password = s.toString();

                if (TextUtils.isEmpty(password)) {
                    ivClear.setVisibility(View.GONE);
                } else {
                    ivClear.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.iv_nav_back, R.id.iv_accountClear,
            R.id.iv_clear, R.id.btn_login, R.id.tv_register,R.id.tv_forgetPassword})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_nav_back:
                finish();
                break;
            case R.id.iv_accountClear:
                etAccount.setText("");
                break;
            case R.id.iv_clear:
                etPassword.setText("");
                break;
            case R.id.btn_login:

                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                login(account, password);

                break;
            case R.id.tv_register://注册
                intent = new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_forgetPassword:
                intent = new Intent(this,ForgetPWActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     */
    private void login(final String account, final String password) {
        api.login(account, password, new IBaseRequestImp<Nullable>() {
            @Override
            public void onRequestSuccess(Nullable nullable) {
                if (cbRememberPw.isChecked()){
                    LoginBean loginBean = new LoginBean(account, password);
                    SPUtil.save(LoginActivity.this,SPUtil.ACCOUNT_INFO,SPUtil.ACCOUNT_INFO_KEY,new Gson().toJson(loginBean));
                }

                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
